home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AppsToGo / Kibitz / GoToMove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  4.7 KB  |  200 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        gotomove.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __PACKAGES__
  21. #include <Packages.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32. #ifndef __UTILITIES__
  33. #include <Utilities.h>
  34. #endif
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41.  
  42. #define kGoToOK        1
  43. #define kGoToEquivs    2
  44. #define kGoToCancel    3
  45. #define kStatText1    4
  46. #define kWhichMove    5
  47. #define kWhiteMove    6
  48. #define kBlackMove    7
  49.  
  50. #define kLeftArrow    28
  51. #define kRightArrow    29
  52. #define kUpArrow    30
  53. #define kDownArrow    31
  54.  
  55. static short    gOption;
  56.  
  57. static pascal Boolean    GoToFilter(DialogPtr dlg, EventRecord *event, short *item);
  58.  
  59.  
  60.  
  61. /*****************************************************************************/
  62. /*****************************************************************************/
  63.  
  64. #ifdef applec
  65. #pragma segment GoToMove
  66. #endif
  67.  
  68. /*****************************************************************************/
  69. /*****************************************************************************/
  70.  
  71.  
  72.  
  73. void    DoGoToMove(FileRecHndl frHndl)
  74. {
  75.     WindowPtr        oldPort, window;
  76.     DialogPtr        dialog;
  77.     short            item, userItemType, startColor, halfMoveNum, moveNum;
  78.     short            gameIndex, whosMove, i;
  79.     Handle            itemHndl;
  80.     Rect            userItemRect;
  81.     TEHandle        te;
  82.     Str255            str;
  83.     TheDocPtr        docPtr;
  84.     static ModalFilterUPP goToFilterUPP = nil;
  85.  
  86.     oldPort = SetFilePort(frHndl);
  87.  
  88.     window = (*frHndl)->fileState.window;
  89.     dialog = GetCenteredDialog(rGoToMove, nil, window, (WindowPtr)-1L);
  90.     if (dialog) {
  91.  
  92.         startColor  = (*frHndl)->doc.startColor;
  93.         halfMoveNum = (*frHndl)->doc.gameIndex + startColor;
  94.         moveNum     = (halfMoveNum >> 1);
  95.  
  96.         item = kWhiteMove + (halfMoveNum - 2 * moveNum);
  97.         GetDialogItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  98.         SetControlValue((ControlHandle)itemHndl, true);
  99.  
  100.         te = ((DialogPeek)dialog)->textH;
  101.         pcpydec(str, moveNum + 1);
  102.         TESetText(str + 1, *str, te);
  103.         TESetSelect(0, *str, te);
  104.  
  105.         for (;;) {
  106.  
  107.             SetPort(dialog);
  108.             if (!goToFilterUPP)
  109.                 goToFilterUPP = NewModalFilterProc (GoToFilter);
  110.             ModalDialog(goToFilterUPP, &item);
  111.  
  112.             if (item == kGoToOK) {
  113.                 GetDialogItem(dialog, kBlackMove, &userItemType, &itemHndl, &userItemRect);
  114.                 whosMove = GetControlValue((ControlHandle)itemHndl);
  115.                 BlockMove(*((*te)->hText), str + 1, *str = (*te)->teLength);
  116.                 break;
  117.             }
  118.  
  119.             if (item == kGoToCancel) break;
  120.  
  121.             if (item >= kWhiteMove) {
  122.                 GetDialogItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  123.                 SetControlValue((ControlHandle)itemHndl, true);
  124.                 item = (kWhiteMove + kBlackMove) - item;
  125.                 GetDialogItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  126.                 SetControlValue((ControlHandle)itemHndl, false);
  127.             }
  128.         }
  129.  
  130.         DisposeDialog(dialog);
  131.  
  132.         if (item == kGoToOK) {
  133.             moveNum   = p2num(str, 10, nil) - 1;
  134.             gameIndex = ( 2 * moveNum) + whosMove - startColor;
  135.             if (gOption) {
  136.                 moveNum = gameIndex = 0;
  137.                 docPtr  = &((*frHndl)->doc);
  138.                 if (docPtr->timeLeft[0] != -1)
  139.                     for (i = 0; i < 2; ++i)
  140.                         docPtr->displayTime[i] = docPtr->timeLeft[i] = docPtr->defaultTime[i];
  141.             }
  142.             if ((moveNum >= 0) && (gameIndex >= 0)) {
  143.                 SetFilePort(frHndl);
  144.                 RepositionBoard(frHndl, gameIndex, true);
  145.                 ImageDocument(frHndl, true);
  146.                 AdjustGameSlider(frHndl);
  147.                 if ((*frHndl)->doc.twoPlayer) SendGame(frHndl, kResync, nil);
  148.                 SendMssg(frHndl, kTimeMssg);
  149.                 (*frHndl)->doc.timerRefTick = TickCount();
  150.                 DrawTime(frHndl);
  151.             }
  152.         }
  153.     }
  154.  
  155.     SetPort(oldPort);
  156. }
  157.  
  158.  
  159.  
  160. /*****************************************************************************/
  161.  
  162.  
  163.  
  164. pascal Boolean    GoToFilter(DialogPtr dlg, EventRecord *event, short *item)
  165. {
  166.     TEHandle                te;
  167.     char                    key;
  168.     Boolean                    arrowKey;
  169.     static unsigned long    lastIdle;
  170.  
  171.     gOption = (event->modifiers) & optionKey;
  172.  
  173.     if (KeyEquivFilter(dlg, event, item)) return(true);
  174.  
  175.     *item = 0;
  176.  
  177.     switch (event->what) {
  178.         case keyDown:
  179.         case autoKey:
  180.             te       = ((DialogPeek)dlg)->textH;
  181.             key      = event->message & charCodeMask;
  182.             arrowKey = ((key >= kLeftArrow) && (key <= kDownArrow));
  183.             if ((arrowKey) || (key == 8)) return(false);    /* These are always okay. */
  184.             if ((key < '0') || (key > '9')) return(true);    /* Don't accept non-digits. */
  185.             if ((*te)->teLength >= 3) return(true);            /* Don't accept more than 3 chars. */
  186.             break;
  187.     }
  188.  
  189.     if (lastIdle + 30 < TickCount()) {
  190.         lastIdle = TickCount();
  191.         DoIdleTasks(false);
  192.         SetPort(dlg);
  193.     }
  194.  
  195.     return(false);
  196. }
  197.  
  198.  
  199.  
  200.